<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/11
 * Time: 11:48
 */

namespace app\index\controller;


use app\common\controller\IndexBase;
use app\common\model\Manager;
use app\common\model\Work;

class User extends IndexBase
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->assign('title_head','个人中心');
    }

    public function index(){

        $manager = new Manager();
        $manage_info = $manager->find(3);
        if(in_array(session('user_info')['userid'],json_decode($manage_info['list'],true))){
           $this->assign('manager',1);
        }

        $user_info =model('user')->find(session('user_info'));
        $unread_count = model('message')->where(['user_id'=>session('user_info')['userid'],'status'=>0])->count();
        $this->assign('unread_count',$unread_count);
        $this->assign('user_info',$user_info);
        return $this->fetch();
    }

    /**获取部门列表
     * @return mixed
     */
    public function get_department_list(){
        if($this->request->isAjax()){
            $m = new Work();
            return $m->get_department_list();
        }
    }

    /**根据部门ID获取成员列表
     * @return mixed
     */
    public function get_userlist_by_departmentid(){
        if($this->request->isAjax()){
            $m = new \app\common\model\User();
            return $m->getLists();
        }
    }

    public function sign(){
        //签到时间表
        $list = model('Sign')->where(['user_id'=>session('user_info')['userid']])->column('sign_time');
        //注册时间时长
        $user_info = model('user')->find(session('user_info'));
        $reg_times = time() - strtotime($user_info['add_time']);
        $days = $reg_times/60/60/24;
        if(($days - count($list))>0){
            $lq = $days - count($list);
        }else{
            $lq = 0;
        }
        $this_month = strtotime(date('Y-m-1 00:00:00',time()));
        $thissign = model('Sign')->field('count(id) as count')->where(['user_id'=>session('user_info')['userid'],'sign_time'=>['>=',$this_month]])->find();
        $lq_day = date('d')-$thissign['count'];

       // halt($lq_day);
        $this->assign('lq_day',$lq_day);
        $this->assign('lq',$lq);
        $this->assign('list',$list);
        $this->assign('thissign',$thissign);
        $this->assign('title_head','签到');
        return $this->fetch();
    }

    public function get_sign(){
        $list = model('Sign')->where(['user_id'=>session('user_info')['userid']])->column('sign_time');
        return $list;
    }

    public function to_sign(){
        $times = strtotime(date('y-m-d'));
        $query = model('Sign')->where(['user_id'=>session('user_info')['userid'],'sign_time'=>$times])->find();
        if($query){
            return ['code'=>0];
        }
        $res = model('Sign')->save(['user_id'=>session('user_info')['userid'],'sign_time'=>$times]);
        if($res!==false){
            return ['code'=>1];
        }
        return ['code'=>0];
    }

    public function message(){
        $this->assign('title_head','消息列表');
        return $this->fetch();
    }

    public function get_message_list(){
        $page = input('page')?input('page'):1;
        $list = model('message')->where(['user_id'=>session('user_info')['userid']])->order('status,add_time desc')->page($page,15)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function mytopic(){
        $this->assign('title_head','我的主题');
        return $this->fetch();
    }

    public function get_mytopic(){
        $page = input('page')?input('page'):1;
        $list = model('topic')->where(['author'=>session('user_info')['userid']])->page($page,15)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function mycomment(){
        $this->assign('title_head','我回复的主题');
        return $this->fetch();
    }

    public function get_mycomment(){
        $page = input('page')?input('page'):1;
        $comment = model('comment')->where(['user_id'=>session('user_info')['userid']])->column('topic_id');
        $list = model('topic')->where(['author'=>session('user_info')['userid'],'id'=>['in',$comment]])->page($page,15)->select();
        $this->assign('list',$list);
        return $this->fetch('get_mytopic');
    }
    public function myappreciate(){
        $this->assign('title_head','我点赞的主题');
        return $this->fetch();
    }

    public function get_myappreciate(){
        $page = input('page')?input('page'):1;
        $comment = model('appreciate')->where(['user_id'=>session('user_info')['userid'],'type'=>1])->column('topic_id');
        $list = model('topic')->where(['author'=>session('user_info')['userid'],'id'=>['in',$comment]])->page($page,15)->select();
        $this->assign('list',$list);
        return $this->fetch('get_mytopic');
    }
    public function mycollect(){
        $this->assign('title_head','我收藏的主题');
        return $this->fetch();
    }

    public function get_mycollect(){
        $page = input('page')?input('page'):1;
        $comment = model('appreciate')->where(['user_id'=>session('user_info')['userid'],'type'=>2])->column('topic_id');
        $list = model('topic')->where(['author'=>session('user_info')['userid'],'id'=>['in',$comment]])->page($page,15)->select();
        $this->assign('list',$list);
        return $this->fetch('get_mytopic');
    }

    public function integral(){
        $user_info =model('user')->find(session('user_info'));
        $this->assign('user_info',$user_info);
        $this->assign('title_head','积分');
        return $this->fetch();
    }

    public function integral_log(){
        return $this->fetch();
    }
    public function get_integral_log(){
        $page = input('page')?input('page'):1;
        $list = model('integral_log')->where(['user_id'=>session('user_info')['userid']])->order('add_time desc')->page($page,15)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function integral_list(){
        $list = model('user')->order('integral desc')->select();

        foreach ($list as $k=>$v){
            $res = get_user_info($v['user_id']);
           // if($res['errcode']==0){
                $list[$k]['user'] = $res;
           // }
        }
        $this->assign('list',$list);
        $this->assign('title_head','积分榜');
        return $this->fetch();
    }


    public function redirect_url(){
        $message_id = input('id');
        model('message')->where(['id'=>$message_id])->update(['status'=>1]);
        $message_info = model('message')->find($message_id);
        $this->redirect($message_info['redirect_url']);
    }

    public function all_read(){
        model('message')->where(['user_id'=>session('user_info')['userid'],'status'=>0])->update(['status'=>1]);
        //return 1;
    }

    public function update_info(){
        $user_info = get_user_info_new(session('user_info')['userid']);
        $save_data['avatar'] =$user_info['avatar'];
        $save_data['name'] =$user_info['name'];
        if(model('user')->isUpdate(true)->save($save_data,['user_id'=>session('user_info')['userid']])!==false){
            return ajax_return(1,'更新成功');
        }else{
            return ajax_return(0,'更新失败');
        }
     }
	  public function updatePassword()
    {
        if ($this->request->isAjax()){
            $old_password = input('oldpassword');
            $password = input('password');
            $new_password = password_hash($password,PASSWORD_DEFAULT);
            if (mb_strlen($password)<6){
                return ajax_return(0,'密码长度不能小于6位');
            }
            $pass = db('salary_user')->where(['id'=>session('user_info')['userid']])->value('password');
            if (!$pass && $old_password == '123456'){
                $res = db('salary_user')->where(['id'=>session('user_info')['userid']])->update(['password'=>$new_password]);
            }
            if ($pass && password_verify($old_password,$pass)){
                $res = db('salary_user')->where(['id'=>session('user_info')['userid']])->update(['password'=>$new_password]);
            }
            if ($pass && !password_verify($old_password,$pass)){
                return ajax_return(0,'旧密码错误');
            }

              //过滤非数据表字段数据
            if($res !== false){
                return ajax_return(1,'更新成功');
            }else{
                return ajax_return(0,'更新失败');
            }


        }
        return $this->fetch('updatepassword');
     }

}